﻿using SimpleSpider.Framework.Model;
using SimpleSpider.Framework.Services.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace SimpleSpider
{
    public partial class StartUrlFrm : Form
    {
        public bool IsExecute { get; set; }

        StartUrlService _startUrlService = null;
        public StartUrlFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LoadData()
        {
            textBox1.Text = string.Empty;
            var results =  _startUrlService.GetAll();
            foreach (var result in results)
            {
                textBox1.AppendText(result.Url + "\r\n");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string[] lines = textBox1.Lines;

                _startUrlService.DeleteTable();

                List<StartUrlModel> startUrls = new List<StartUrlModel>();

                foreach (var item in lines)
                {
                    if (string.IsNullOrEmpty(item)) continue;

                    StartUrlModel startUrlModel = new StartUrlModel();
                    startUrlModel.Url = item.Trim();
                    startUrlModel.UniqueId = item.Trim();
                    startUrlModel.Status = Reborn.Contracts.State.TaskStatus.WaitingForActivation;
                    startUrls.Add(startUrlModel);
                }

                _startUrlService.Insert(startUrls);

                MessageBox.Show("操作完成");

                this.Close();

                IsExecute = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误，错误信息：" + ex.Message);
            }
        }

        private void StartUrlFrm_Load(object sender, EventArgs e)
        {
            _startUrlService = new StartUrlService();
            LoadData();
        }
    }
}
